package sgg_hsp_data_structures._02_stack_queue;

import java.util.Scanner;

public class CircleArrayQueueDemo {

    public static void main(String[] args) {

        CircleArrayQueue arrayQueue = new CircleArrayQueue(4);

        char key = ' ';
        Scanner scanner = new Scanner(System.in);


        boolean loop = true;
        while (loop) {

            System.out.println("************WELCOME************");
            System.out.println("s(show):");
            System.out.println("e(exit):");
            System.out.println("a(add) :");
            System.out.println("g(get) :");
            System.out.println("h(head):");
            System.out.println("*******************************");

            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'e':
                    loop = false;
                    break;
                case 'a':
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    int res = arrayQueue.getQueue();
                    System.out.println("get :" + res);
                    break;
                case 'h':
                    int res1 = arrayQueue.headQueue();
                    System.out.println("head :" + res1);
                    break;
                default:
                    break;
            }
        }
    }
}


/**
 * front 随输出变化
 * rear  随输入变化
 * 环形队列有一个位置是空出来的
 */
class CircleArrayQueue {
    private int maxSize;

    /**
     * 1。front指向队列的第一个元素，也就是说arr[front]就是队列的第一个元素，初始值为0；
     * 2。rear指向队列最后一个元素的后一个位置，
     * 3。当队列满时 (rear+1) % maxSize = front
     * 4。队列为空时，rear = front
     * 5。队列中数据个数是：(rear + maxSize  - front) % maxSize
     */
    private int front;
    private int rear;

    private int[] arr;

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = 0;
        rear = 0;
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("Full");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    public int getQueue() {
        if (isEmpty()) {
            System.out.println("Empty");
            throw new RuntimeException("Empty");
        }
        /**
         * 分析得：
         * 1。front是指向队列的第一个元素；
         * 2。讲front后移，考虑取模
         * 3。将临时保存的值返回
         *
         */
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("Empty");
        }

        for (int i = front; i < size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    private int size() {
        return (rear + maxSize - front) % maxSize;
    }

    public int headQueue() {
        if (isEmpty()) {
            System.out.println("Empty");
            throw new RuntimeException("Empty");
        }
        return arr[front];
    }


}
